package org.sanguine.study.hometasks.first.task.a;

import java.util.Arrays;
import java.util.Comparator;

import org.sanguine.study.hometasks.first.Runner;

public class TaskA2 extends CommonStringTask {
	
	public static void main(String[] args) {
		Runner runner = new TaskA2();
		runner.run(args);
	}
	
	@Override
	public void doTask() {
		if (in == null || out == null || numberOfItems <= 0) return;
		readItems();
		if (items != null) sort();
		printAscendingResult();
		printDescendingResult();		
	}

	private void sort() {
		Arrays.sort(items, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				if (o1.length() < o2.length()) return -1;
				else if (o1.length() == o2.length()) return 0;
				else return 1;
			}
		});
	}

	private void printAscendingResult() {
		out.println("Ascending:");
		for (String s : items) out.println(s);
	}

	private void printDescendingResult() {
		out.println("Descending:");
		for (int i = items.length - 1; i >= 0; i--) {
			out.println(items[i]);
		}
	}

}
